package com.haiyou.sdk;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import com.haiyou.common.console.Console;
import com.haiyou.common.constants.ServerConfigConstants;
import com.haiyou.common.constants.ServerTypeConstants;
import com.haiyou.common.system.Properties;
import com.haiyou.common.system.SystemProperties;
import com.haiyou.common.util.string.StringFormatUitls;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@SpringBootApplication
@ComponentScan(value= {
		"com.haiyou.sdk",
		"com.haiyou.common",
		"com.haiyou.data.common",
		"com.haiyou.data.sdk",
		"com.haiyou.data.boss",
		"com.haiyou.data.config",
		"com.haiyou.data.game"
		})
public class SdkApplication {
	
	public static String nodeType = ServerTypeConstants.server_game;
	
	/**
	 * 启动方法
	 * @param args
	 */
	public static void main(String[] args) {
		
		try {
			
			//加载配置文件
			loadSystemProperties();
			
			//设置参数
			setProperty();
			
			//启动应用
			SpringApplication.run(SdkApplication.class,args);
			
			Console.listenerInputExit();

		}catch (Exception e) {
			log.info("启动服务器发生异常,启动服务器失败");
			log.error(e.getMessage(), e);
			System.exit(0);
		}
	}
	
	/**
	 * 设置一些类型
	 */
	public static void setProperty() {

		System.setProperty("spring.devtools.restart.enabled", "false");

		// 设置节点类型
		System.setProperty(ServerConfigConstants.server_node_type, nodeType);

		String nameFormat = "{}({})";

		String name = StringFormatUitls.format(nameFormat, nodeType, Properties.server_node_id());

		// 设置应用名称
		System.setProperty(ServerConfigConstants.spring_application_name, name);
	}
	
	/**
	 * 加载配置文件
	 */
	public static void loadSystemProperties() {
		
		SystemProperties.load(SystemProperties.getResourcesAbsolutePath(
				ServerConfigConstants.spring_path,ServerConfigConstants.config_properties));
	}
	
}
